<template>
  <el-dialog v-bind="$attrs" v-on="$listeners">
    <el-form
      :model="params"
      :rules="departmentRules"
      ref="form"
      label-width="100px"
      size="mini"
    >
      <el-form-item label="院系名称:" prop="department_name">
        <el-input v-model="params.department_name"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button size="mini" @click="change()">取消</el-button>
      <el-button type="primary" size="mini" @click="confirm()">添加</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { insertNewDepartment } from "@/api/department";
import departmentRules from "@/rules/departmentRules.js";
export default {
  data() {
    return {
      departmentRules,
      params: {
        department_name: "",
      },
    };
  },
  methods: {
    change() {
      // 重置表单数据
      this.$refs.form.resetFields();
      // 重置表单状态
      this.$refs.form.clearValidate();
      this.$emit("update:visible", false);
    },
    async confirm() {
      await this.$refs.form.validate();
      await insertNewDepartment(this.params);
      this.$emit("reload");
      this.$emit("update:visible", false);
      // this.params = {};
      // 重置表单数据
      this.$refs.form.resetFields();
      // 重置表单状态
      this.$refs.form.clearValidate();
    },
  },
};
</script>
